<?php 
/**
 * User: Lijun
 * Date: 2017/7/4.
 * Time: 15:46.
 * Comment:
 */

namespace Admin\Controller;
use Think\Controller;

class DownloadController extends Controller{
	public function index(){
		if($_GET['name']){
			$name = $_GET['name'];
			$map['name'] = array('like',"%{$name}%");
		}
		$count = M('download') -> where($map) -> count();
		$page = new \Think\Page($count,10);
		$show = $page -> show();
		$download = M('download') ->where($map) ->limit($page -> firstRow.','.$page -> listRows) -> order("id desc")-> select();
                foreach($download as $k => $v){
                    $download[$k]['pid'] = M("category") -> where(array("id" => $v["pid"])) -> getField("catname");
                }
		$this -> assign('download',$download);
		$this -> assign('page',$show);
		$this -> assign('name',$name);
		$this -> display();
	}

	public function addDownload(){
		$categorys = M('category') ->  where('id in (34,35,36)') -> select();
		$this -> assign('category',$categorys);
		$this -> display();
	}

	public function doAddDownload(){
            $upload = new \Think\Upload();
            $upload->maxSize   =     1048576000 ;// 设置附件上传大小
            $upload->rootPath  =     './Public/'; // 设置附件上传根目录
            $upload->savePath  =     'uploads/file/'; // 设置附件上传（子）目录
            // 上传文件
            $file   =   $upload->upload();
            if(!$file){
        	$this -> error('文件上传失败');
            }else{
                $info['pid'] = I("post.pid");
                $info['name'] = $file['file']['name'];
                $info['path'] =  $file['file']['savepath'].$file['file']['savename'];
                $info['time'] = time();
                if(M('download') -> add($info)){
                    $this -> success('上传成功',U('index'));
                }else{
                    $this -> error('上传失败');
                }
            }
	}

	public function delDownload(){
		if(IS_GET){
			$id = I('id');
			if(M('download') -> where("id = {$id}") -> delete()){
				$this -> success('删除成功',U('index'));
			}else{
				$this -> error('删除失败');
			}
		}

		if(IS_POST){
			$ids = I('id');
			$id = implode(',', $ids);
			if(M('download') -> where("id in ({$id})") -> delete()){
				$this -> success('删除成功',U('index'));
			}else{
				$this -> error('删除失败');
			}
		}
	}
}

 ?>